package com.linzelin.www.po;

import com.linzelin.www.util.Activity;
import com.linzelin.www.util.Charger;
import com.linzelin.www.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Scanner;


public class Account_Charger {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //先获取一波连接池
    public boolean login(String username,String password) {
        if (username == null || password == null) {
            return false;
        }
        String sql = "select * from charger ";
        //获取表中符合条件的账号,有就返回true,没有就返回false
        List<Charger> list = template.query(sql,new BeanPropertyRowMapper<Charger>(Charger.class));
        for (Charger c : list) {
            if(c.getUsename().equals(username))
            {
                while(!(c.getPassword().equals(password)))
                {
                    System.out.println("密码有误,重新输入");
                    password = new Scanner(System.in).next();
                }
                return true;
            }
        }
        return false;
    }

    public void Land(Charger charger) throws  InterruptedException {
        /*Properties prop_find = new Properties();
        prop_find.load(new FileReader("C:\\Users\\啦啦啦\\Desktop\\Charger.txt"));
        Set<String> set = prop_find.stringPropertyNames();*/
        while(!login(charger.getUsename(),charger.getPassword()))
        {
            System.out.println("输入的账号或密码有误,请重新输入");
            System.out.println("账号:");
            charger.setUsename(new Scanner(System.in).next());
            System.out.println("密码:");
            charger.setPassword(new Scanner(System.in).next());
        }
        System.out.println("登录成功");
        /*while(true)
        {
            for (String s : set) {
                if(s.equals(charger.getName()))
                {
                    if(prop_find.get(s).equals(charger.getCipher()))
                    {
                        System.out.println("正在登入\n=================");
                        Thread.sleep(2000);
                        return;
                    }
                    else
                    {
                        String code;
                        while (!(prop_find.get(s).equals(code = new Scanner(System.in).next()))){
                            System.out.println("密码错误，请重新输入密码");
                        }
                        charger.setCipher(code);
                        System.out.println("正在登入\n=================");
                        Thread.sleep(2000);
                        return;
                    }
                }
            }
            System.out.println("无效账号，请重新登录");
            System.out.println("账号");
            charger.setName(new Scanner(System.in).next());
            System.out.println("密码");
            charger.setCipher(new Scanner(System.in).next());
        }*/
    }

    public void Logon(Charger charger) throws Exception{
        /*prop.setProperty(charger.getName(),charger.getCipher());
        FileWriter fw = new FileWriter("C:\\Users\\啦啦啦\\Desktop\\Charger.txt",true);
        prop.store(fw,"新管理员");*/
        String sql ="insert into charger values (null,?,?,1)";
        template.update(sql,charger.getUsename(),charger.getPassword());
        System.out.println("正在注册\n=================");
        Thread.sleep(2000);
        System.out.println("注册成功");
    }

    public void Operation(Charger charger) throws Exception {
        System.out.println("请选择服务:\n1.添加活动\n2.审核学生报名\n3.修改活动状态\n4.退出");
        int select = new Scanner(System.in).nextInt();
        switch(select)
        {
            case 1:
                System.out.println("请输入活动名称");
                new Account_activity().Activity_add(new Activity(new Scanner(System.in).next()));
                break;
            case 2:break;
            case 3:
                System.out.println("请输入要查看的活动名称");
                new Account_activity().Activity_enroll(new Scanner(System.in).next());break;
            case 4:break;
        }
    }
}